import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/api.dart';
import 'package:guochaoapp/common/utils/http_client.dart';
import 'package:guochaoapp/common/widges/dialog.dart';
import 'package:guochaoapp/controller/mine/mine_controller.dart';
import 'package:guochaoapp/model/cart_settle_entity.dart';
import 'package:guochaoapp/model/order_detail_entity.dart';
import 'package:guochaoapp/routes/app_pages.dart';
import 'package:guochaoapp/services/user_service.dart';

import '../../r.dart';
import '../tabbar_controller.dart';

class GCOrderDetailController extends GetxController {
  UserService userService = Get.find();

  var orderDetail = OrderDetailData().obs;

  getWeightTotal() {
    double weightTotal = orderDetail.value.weightTotal;
    if (weightTotal >= 1000) {
      return (weightTotal / 1000).toStringAsFixed(2) + "吨";
    } else {
      return weightTotal.toStringAsFixed(2) + "千克";
    }
  }

  Future requestInitData(String mallOrderNo) async {
    EasyLoading.show();
    await HttpClient.request<OrderDetailEntity>(
        url: MallApi.orderDetail + mallOrderNo,
        method: Method.GET,
        params: {"uid": userService.userInfo.uid},
        onSuccess: (_, model) async {
          orderDetail.value = model.data;
          EasyLoading.dismiss();
          orderDetail.refresh();
        },
        onFailed: (err) async {
          EasyLoading.showError(err);
        });
    return null;
  }

  void jumpToList(List<CartSettleDataItemList> itemList) {
    Get.toNamed(Routes.SHOPPING_LIST, arguments: itemList);
  }

  void cancelOrder() {
    showDialog(
        context: Get.context,
        barrierDismissible: false,
        builder: (ctx) {
          return GCDialog(
            items: ['取消', '确认'],
            content: '确认取消订单？',
            onTap: (_) {
              EasyLoading.show();
              HttpClient.request(
                  url: MallApi.cancelOrder,
                  method: Method.POST,
                  data: {
                    "uid": userService.userInfo.uid,
                    "mallOrderNo": orderDetail.value.mallOrderNo
                  },
                  onSuccess: (_, model) async {
                    EasyLoading.dismiss();
                    Get.back();
                    GCMineController mineController = Get.find();
                    mineController.requestOrderList(false, false);
                  },
                  onFailed: (err) async {
                    EasyLoading.showError(err);
                  });
            },
          );
        });
  }

  void orderReturn() {
    showDialog(
        context: Get.context,
        barrierDismissible: false,
        builder: (ctx) {
          return GCDialog(
            items: ['取消', '继续申请'],
            content: '申请退货期间无法申请开票\n是否继续申请',
            title: Image.asset(R.assetsIconWarning, height: 27, width: 27),
            onTap: (_) {
              EasyLoading.show();
              HttpClient.request(
                  url: MallApi.returnOrder,
                  method: Method.POST,
                  data: {
                    "uid": userService.userInfo.uid,
                    "mallOrderNo": orderDetail.value.mallOrderNo
                  },
                  onSuccess: (_, model) async {
                    EasyLoading.dismiss();
                    Get.toNamed(Routes.ORDER_RETURN);
                  },
                  onFailed: (err) async {
                    EasyLoading.showError(err);
                  });
            },
          );
        });
  }

  void jumpToReturn() {
    Get.toNamed(Routes.ORDER_RETURN);
  }

  void returnDetail() {
    Get.toNamed(Routes.ORDER_RETURN_DETAIL, arguments: orderDetail.value);
  }

  void orderInvoice() {
    Get.toNamed(Routes.ORDER_INVOICE, arguments: false);
  }

  void jumpToInvoice() {
    Get.toNamed(Routes.ORDER_INVOICE, arguments: true);
  }

  void jumpToPayment() {
    Get.toNamed(Routes.SHOPPING_PAYMENT, arguments: {
      "price": orderDetail.value.paymentPriceTotal,
      "mallOrderNo": orderDetail.value.mallOrderNo
    });
  }

  void copyTransfer() {
    EasyLoading.showSuccess("复制成功");
    Clipboard.setData(ClipboardData(
        text:
            "公司名称:${orderDetail.value.receiptCardDTO.companyName}\n银行:${orderDetail.value.receiptCardDTO.bankName}\n"
                "分行:${orderDetail.value.receiptCardDTO.branchName} \n卡号:${orderDetail.value.receiptCardDTO.cardNo}"));
  }
}
